/*
    Android UIML

	Copyright (C) 2010  Bram Goffings (bramgoffings@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public License
	as published by the Free Software Foundation; either version 2.1
	of	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package uiml.android.executing;

import java.util.ArrayList;

import org.w3c.dom.Element;

import uiml.android.elements.Logic;
import uiml.android.elements.Part;
import uiml.android.elements.Property;
import uiml.android.elements.UimlElement;
import uiml.android.elements.UimlElementWithAttributes;
import uiml.android.elements.peers.Vocabulary;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Action extends UimlElementWithAttributes{

	// TODO comment
	private ArrayList<UimlElement>	m_subActions = new ArrayList<UimlElement>();
	
	// TODO comment
	Object invoker = null;
	Part subTree = null;
	Vocabulary voc = null;
	Logic logic = null;

	// TODO comment
	public static String IAM = "action";
	public static String CALL = "call";
	public static String PROPERTY = "property";
	public static String EVENT = "event";
	
	/**
	 * 
	 * @param node
	 */
	public Action(Element node){
		super(node);
		process();
	}

	/**
	 * 
	 */
	@Override
	public void processElement(Element elem) {
		if (elem.getTagName().equals(CALL))
			m_subActions.add(new Call(elem));
		else if (elem.getTagName().equals(PROPERTY))
			m_subActions.add(new Property(elem));
        else if (elem.getTagName().equals(EVENT))
            m_subActions.add(new Event(elem));
	}
	
	/**
	 * 
	 * @param obj
	 * @param uiPart
	 */
	public void execute() {
		
		for(int i=0; i<m_subActions.size(); ++i) {	
			if(m_subActions.get(i).getClass() == Property.class)
				((Property)m_subActions.get(i)).executeProperty((View)invoker, subTree, voc);
			else if(m_subActions.get(i).getClass() == Call.class)
				((Call)m_subActions.get(i)).execute((View)invoker, subTree, voc);	
		}		
	}

	/**
	 * 
	 * @param obj
	 * @param uiPart
	 * @param property
	 */
	public void addEventHandler(String event){
		
		if(event.equals("ButtonPressed")) {
			((View)invoker).setOnClickListener(new OnClickListener(){ 
				public void onClick(View v){ 
					execute();
				}
			});
		}
		else if(event.equals("ButtonLongPressed")) {
			((View)invoker).setOnLongClickListener(new OnLongClickListener(){ 
				public boolean onLongClick(View v){ 
					execute();
					return false;
				}
			});
		}
		else if(event.equals("SeekBarChanged")) {
			((SeekBar)invoker).setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			    public void onProgressChanged(SeekBar seekBar, int progress,
				 boolean fromUser) {
			    	execute();
				}
				public void onStartTrackingTouch(SeekBar arg0) {} //empty
				public void onStopTrackingTouch(SeekBar seekBar) {} //empty
				
			});
		}
		
	}

	/**
	 * @return the invoker
	 */
	public Object getInvoker() {
		return invoker;
	}

	/**
	 * @param invoker the invoker to set
	 */
	public void setInvoker(Object invoker) {
		this.invoker = invoker;
	}

	/**
	 * @return the subTree
	 */
	public Part getSubTree() {
		return subTree;
	}

	/**
	 * @param subTree the subTree to set
	 */
	public void setSubTree(Part subTree) {
		this.subTree = subTree;
	}
	
	/**
	 * @return the voc
	 */
	public Vocabulary getVoc() {
		return voc;
	}

	/**
	 * @param voc the voc to set
	 */
	public void setVoc(Vocabulary voc) {
		this.voc = voc;
	}

	/**
	 * @return the voc
	 */
	public Logic getLogic() {
		return logic;
	}

	/**
	 * @param voc the voc to set
	 */
	public void setLogic(Logic logic) {
		this.logic = logic;
	}


}
